<?php

define('Log_DirectionIn', 		1);
define('Log_DirectionOut', 		2);
define('Log_DirectionSys', 		3);
define('Log_DirectionDebug',	4);

define('Log_TypeMsg',			1);
define('Log_TypeMsgQuery',		2);
define('Log_TypeMode', 			4);
define('Log_TypeOthers',	   64);

define('Log_TypeDebug',		16384);
define('Log_TypeSys', 		32768);

define('Log_TypeRaw', 		  127);
define('Log_TypeAll', 		65535);


define('Log_TargetOutput',	 	0);
define('Log_TargetFile',	 	1);
define('Log_TargetSQLite',	 	2);

class Log {
	
	private $Methods = array();
	private $Prefix =array(	Log_DirectionIn 	=>'<',
							Log_DirectionOut 	=>'>',
							Log_DirectionSys	=>'Sys>',
							Log_DirectionDebug	=>'Debug>'
						 );
	
	
	public function ToFile($filename,$items)
	{
		$items=$items & 65535; // to word conversion
		$this->Methods[$this->GetNewMID()]=array(
											'type'=>Log_TargetFile,
											'items'=>$items,
											'settings'=>array('filename'=>$filename)
											);
	}
	
	public function ToOutput($items)
	{
		$items=$items & 65535; // to word conversion
		$this->Methods[$this->GetNewMID()]=array(
											'type'=>Log_TargetOutput,
											'items'=>$items,
											'settings'=>array('filename'=>$filename)
											);
	}
	
	public function ToSQLite($filename,$items)
	{
		$items=$items & 65535; // to word conversion
		$this->Methods[$this->GetNewMID()]=array(
											'type'=>Log_TargetSQLite,
											'items'=>$items,
											'settings'=>array('filename'=>$filename,'handle'=>false)
											);
	}
	
	private function GetNewMID()
  	{
    	$Max=0;
    	while (isset ($this->Methods[$Max])) $Max++; 
    	return $Max;
  	}
  	
	// Returns type od $data
	private function DataType($data)
	{
		switch (true)
		{
			case (ereg('^:([^!]+)!([^ ]+) ([^ ]+) ([^ ]+) :(.*)',$data)):
						return Log_TypeMsgQuery;	
						break;
			case (ereg('^:([^!]+)!([^ ]+) ([^ ]+) [:]?(.+)',$data)):
						return Log_TypeMsg;	
						break;
			default: return Log_TypeOthers;
		}
	}
	
	public function In($data)
	{
		$type=$this->DataType($data);
		$i=0;
		while(isset($this->Methods[$i]))
		{
			if (($type & $this->Methods[$i]['items'])>0)
			{
				// write this log by using $this->Methods[$i] parameters
				$this->Write($data,$i,Log_DirectionIn); 
			}
			$i++;
		}
	}
	
	public function Out($data)
	{
		$type=$this->DataType($data);
		$i=0;
		while(isset($this->Methods[$i]))
		{
			if (($type & $this->Methods[$i]['items'])>0)
			{
				// write this log by using $this->Methods[$i] parameters
				$this->Write($data,$i,Log_DirectionOut); 
			}
			$i++;
		}
	}
	
	public function Sys($data)
	{
		$type=Log_TypeSys;
		$i=0;
		while(isset($this->Methods[$i]))
		{
			if (($type & $this->Methods[$i]['items'])>0)
			{
				// write this log by using $this->Methods[$i] parameters
				$this->Write($data,$i,Log_DirectionSys); 
			}
			$i++;
		}
	}
	public function Debug($data)
	{
		$type=Log_TypeDebug;
		$i=0;
		while(isset($this->Methods[$i]))
		{
			if (($type & $this->Methods[$i]['items'])>0)
			{
				// write this log by using $this->Methods[$i] parameters
				$this->Write($data,$i,Log_DirectionDebug); 
			}
			$i++;
		}
	}
	// Write log by using $MID method
	private function Write($data,$MID,$direction=Log_DirectionIn)
	{
		switch($this->Methods[$MID]['type'])
		{
			case Log_TargetOutput: 	echo $this->Prefix[$direction].$data."\n";
								   	break;
			case Log_TargetFile: 	$this->FileWrite($this->Methods[$MID]['settings']['filename'],$this->Prefix[$direction].$data);
				   				 	break;	
			case Log_TargetSQLite:  $this->SQLiteWrite($this->Methods[$MID]['settings']['filename'],$direction,$data);	
									break;
		}
	}
	
	private function SQLiteWrite($settings, $direction, $data)
	{
	  if ($settings['handle']==false)
	   {	
		if (!file_exists($filename))
		{
			$sl=sqlite_open($filename, 0666);
			if($sl!=FALSE) sqlite_query($sl,'CREATE Table Logs (timestamp int, type tinyint, data text)');
		}
		else $sl=sqlite_open($filename, 0666);
	   }
	  else $sl=$settings['handle'];
	  if($sl!=FALSE) sqlite_unbuffered_query($sl,"INSERT into Logs (timestamp,type,data) VALUES ('".Time()."', '$direction', '$data')"); 
	}
	
	private function FileWrite($filename,$data)
	{
		if (file_exists($filename))	$fp = @fopen($filename,'a');
		else $fp = @fopen($filename,'w');
		if($fp != false)
		{
			@fwrite($fp,date("Y-m-d H:i:s ").$data."\n");
			fclose($fp);
		}
	}
}
?>